'use strict';

const { Service } = require('egg');

class LoginService extends Service {
  async index() {
    const { ctx } = this;
    const {username,password}=ctx.request.body
    const Users=await ctx.model.User.findOne({
        where:{
            username:username
        }
    })
    if(!Users){
        return {
            msg:'用户名不存在',
            code:1
        }
    }
    if (Users.status===1){
      return {
        msg:'账号已被锁定，请联系管理员',
        code:1
      }
    }
    const result=await ctx.helper.comparePassword(password,Users.password)
    if(result){
       ctx.helper
      return {
        msg:'登陆成功',
        data:{
          token:ctx.helper.getToken(Users),
          username:Users.username
        }
      }
    }else{
      return {
        msg:'账号或密码输入错误',
        code:1,
      }
    }
  }
}

module.exports = LoginService;
